package org.eclipse.snippets.code;

import java.util.LinkedList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.LineStyleEvent;
import org.eclipse.swt.custom.LineStyleListener;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SearchStyleText {

	Display display = new Display();
	Shell shell = new Shell(display);
	LinkedList<StyleRange> list;
	StyledText styledText;
	Text keywordText;
	Button button;

	String keyword;

	public SearchStyleText() {
		shell.setLayout(new GridLayout(2, false));

		styledText = new StyledText(shell, SWT.MULTI | SWT.WRAP);
		GridData gridData = new GridData(GridData.FILL_BOTH);
		gridData.horizontalSpan = 2;
		styledText.setLayoutData(gridData);

		keywordText = new Text(shell, SWT.SINGLE | SWT.BORDER);
		button = new Button(shell, SWT.PUSH);
		button.setText("Search");
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				keyword = keywordText.getText();
				styledText.redraw();
			}
		});

		styledText.addLineStyleListener(new LineStyleListener() {
			public void lineGetStyle(LineStyleEvent event) {
				if (keyword == null || keyword.length() == 0) {
					event.styles = new StyleRange[0];
					return;
				}

				String line = event.lineText;
				int cursor = -1;

				list = new LinkedList<StyleRange>();
				while ((cursor = line.indexOf(keyword, cursor + 1)) >= 0) {
					list.add(getHighlightStyle(event.lineOffset + cursor,
							keyword.length()));
				}

				event.styles = (StyleRange[]) list.toArray(new StyleRange[list
						.size()]);
			}
		});

		keyword = "SW";

		shell.pack();
		shell.open();
		// textUser.forceFocus();

		// Set up the event loop.
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				// If no more entries in event queue
				display.sleep();
			}
		}

		display.dispose();
	}

	private StyleRange getHighlightStyle(int startOffset, int length) {
		StyleRange styleRange = new StyleRange();
		styleRange.start = startOffset;
		styleRange.length = length;
		styleRange.background = shell.getDisplay().getSystemColor(
				SWT.COLOR_YELLOW);
		return styleRange;
	}

	public static void main(String[] args) {
		new SearchStyleText();
	}
}
